package com.xiao.watch_tool;

/**
 * 测试死锁线程监控
 *
 * @CreateBy 花和尚  1130365579@qq.com  2023/3/10 17:39
 * @Version 1.0
 */
public class JConsoleTestCaseThreadDeadlock {

    static class SynAddRunnable implements Runnable {
        int a, b;

        public SynAddRunnable(int a, int b) {
            this.a = a;
            this.b = b;
        }

        /**
         * 由于 Integer -128 - 127 做了缓存，所以会字节返回缓存对象
         */
        @Override
        public void run() {
            synchronized (Integer.valueOf(a)) {
                synchronized (Integer.valueOf(b)) {
                    System.out.println(a + b);
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            new Thread(new SynAddRunnable(1, 2)).start();
            new Thread(new SynAddRunnable(2, 1)).start();
        }
    }
}
